package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.sys.common.config.YmlConfig;
import com.yt.sys.models.bo.AssignPermissionsSaveBO;
import com.yt.sys.models.bo.SysRoleSaveBO;
import com.yt.sys.models.dto.SysRoleDTO;
import com.yt.sys.models.dto.SysRolePermissionDTO;
import com.yt.sys.models.dto.SysUserRoleDTO;
import com.yt.sys.models.service.ISysRolePermissionService;
import com.yt.sys.models.service.ISysRoleService;
import com.yt.sys.models.service.ISysUserRoleService;
import com.yt.sys.models.vo.SysRolePermissionVO;
import com.yt.sys.models.vo.SysRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统角色信息控制器
 *
 * @author zhou
 * @Date 2025-09-03 14:47:27
 */

@RestController
@RequestMapping("/api/sysRole")
@Slf4j
@Api(value = "SysRoleApiController", tags = "系统角色信息Api接口")
public class SysRoleApiController {

    @DubboReference
    private ISysRoleService sysRoleService;
    @DubboReference
    private ISysUserRoleService sysUserRoleService;
    @DubboReference
    private ISysRolePermissionService sysRolePermissionService;
    @Resource
    private YmlConfig ymlConfig;

    @PostMapping("/sysRoleAdd")
    @ApiOperation(value = "角色新增", notes = "角色新增")
    public Result<Boolean> sysRoleAdd(@RequestBody SysRoleSaveBO saveBO) {
        boolean flag = sysRoleService.roleAdd(saveBO);
        return new Result<>(flag);
    }

    @PostMapping("/sysRoleDelete")
    @ApiOperation(value = "角色删除", notes = "角色删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> sysRoleDelete(@RequestParam Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //1.删除角色表
        boolean flag = sysRoleService.deleteById(id);
        //2.删除用户角色关系表
        Wrapper<SysUserRoleDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("role_id", id);
        sysUserRoleService.delete(wrapper);
        //3.删除角色权限关系表
        Wrapper<SysRolePermissionDTO> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("role_id", id);
        sysRolePermissionService.delete(wrapper1);
        return new Result<>(flag);
    }

    @PostMapping("/querySysRole")
    @ApiOperation(value = "角色查询", notes = "角色查询")
    public Result<List<SysRoleVO>> querySysRole() {
        List<SysRoleVO> sysRoleVOS = sysRoleService.queryRole();
        return new Result<>(sysRoleVOS);
    }

    @PostMapping("/addSysRole")
    @ApiOperation(value = "新增角色", notes = "新增角色")
    public Result<Boolean> addSysRole(@RequestBody SysRoleSaveBO saveBO) {
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = sysRoleService.add(saveBO);
        return new Result<>(flag);
    }

    @PostMapping("/updateSysRole")
    @ApiOperation(value = "角色编辑", notes = "角色编辑")
    public Result<Boolean> updateSysRole(@RequestBody SysRoleSaveBO saveBO) {
        SysRoleDTO sysRoleDTO = new SysRoleDTO();
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        BeanUtils.copyProperties(saveBO, sysRoleDTO);
        boolean flag = sysRoleService.updateById(sysRoleDTO);
        return new Result<>(flag);
    }

    @PostMapping("/assignPermissions")
    @ApiOperation(value = "权限分配", notes = "权限分配")
    @Transactional
    public Result assignPermissions(@RequestBody AssignPermissionsSaveBO saveBO) {
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Wrapper<SysRolePermissionDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("role_id", saveBO.getRoleId());
        sysRolePermissionService.delete(wrapper);
        List<Long> permissIdList = saveBO.getPermIds();
        for (Long permissId : permissIdList) {
            SysRolePermissionDTO sysRolePermissionDTO = new SysRolePermissionDTO();
            sysRolePermissionDTO.setId(ymlConfig.getPkId());
            sysRolePermissionDTO.setRoleId(saveBO.getRoleId());
            sysRolePermissionDTO.setPermissionId(permissId);
            sysRolePermissionService.insert(sysRolePermissionDTO);
        }
        return new Result<>();
    }

    @PostMapping("/queryRolePermissions")
    @ApiOperation(value = "角色权限查询", notes = "角色权限查询")

    public Result<List<SysRolePermissionVO>> queryRolePermissions(@RequestParam Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Wrapper<SysRolePermissionDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("role_id", id);
        List<SysRolePermissionDTO> dto = sysRolePermissionService.selectList(wrapper);
        List<SysRolePermissionVO> voList = new ArrayList<>();
        if (dto == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        for (SysRolePermissionDTO sysRolePermissionDTO : dto) {
            SysRolePermissionVO vo = new SysRolePermissionVO();
            BeanUtils.copyProperties(sysRolePermissionDTO, vo);
            voList.add(vo);
        }
        return new Result<>(voList);
    }
}
